#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from lib.load_data import *
import sys
import re
import time
reload(sys)
sys.setdefaultencoding('utf-8')


#mysql - order
def order0_conn():    
    conn = HandleMysql('192.168.12.81','58885','syzn58user_rd','FwE6dfgtpas12t6m','dbwww58com_pay_order_0')
    conn.connect()
    return conn
#mysql - order
def order1_conn():    
    conn = HandleMysql('192.168.12.81','58885','syzn58user_rd','FwE6dfgtpas12t6m','dbwww58com_pay_order_1')
    conn.connect()
    return conn
#mysql - order
def order2_conn():    
    conn = HandleMysql('192.168.12.81','58886','syzn58user_rd','FwE6dfgtpas12t6m','dbwww58com_pay_order_2')
    conn.connect()
    return conn
#mysql - order
def order3_conn():    
    conn = HandleMysql('192.168.12.81','58886','syzn58user_rd','FwE6dfgtpas12t6m','dbwww58com_pay_order_3')
    conn.connect()
    return conn

def zhiding_conn():
    conn = HandleMysql('10.5.20.16','58885','ec_stat','L#c8UM5RGh5C','dbwww58com_58_stats')
    conn.connect()
    return conn

def get_topinfo_project_order_info(project_id, date):
    num = 0
    conn = zhiding_conn()
    order_id = 0
    q = "select order_id,num from top_info_tmp where top_info_id = %s" % project_id
    its = conn.get_results(q)
    if len(its) > 0:
        num = its[0][1]
        order_id = its[0][0]
    conn.close()

    order_conn = order0_conn()
    if num == 0:
        order_conn = order0_conn()
    elif num == 1:
        order_conn = order1_conn()
    elif num == 2:
        order_conn = order2_conn()
    else:
        order_conn = order3_conn()

    cash_amount = 0.0
    discount_amount = 0.0
    refund_cash_amount = 0.0
    refund_discount_amount = 0.0

    if order_id > 0:
        print "The program of the date of %s is beginning at %s " %(date,get_now())
        q = "select order_money, from_account_type, ifnull(refund_money,0) from sub_order where order_id = '%s'" % order_id
        rs = order_conn.get_results(q)
        if len(rs) > 0:
            for r in rs:
                order_amount = r[0]
                amount_type = r[1]
                refund_money = r[2]
                if amount_type == 3:
                    discount_amount += float(order_amount)
                    refund_discount_amount += float(refund_money)
                else:
                    cash_amount += float(order_amount)
                    refund_cash_amount += float(refund_money)
        print "The program of the date of %s is end at %s " %(date,get_now())

    order_conn.close()
    money = "%s,%s,%s,%s" %(cash_amount, discount_amount,refund_cash_amount,refund_discount_amount)
    print money
    return money

